var ptCollections = {
	cache:{
		cmsAttr:[]
	},
	image:'/themes/taskme/images/5-1.gif',
	id:0,
	open: function(){
		
		jCollections('', '', 'Collections board', function(r) {
			if( r ){
				alert(r)
			}
		});
	},
	data:{
	
	},
	html:{
		li: function(t,_id){
			return '<li><a href="javascript:void(0)" uuid="'+_id+'">'+t+'</a></li>';
		},
		formAddCollection: function(){
			return '<div class="formaddcol"><input type="text" name="name"> <a href="javascript:void(0)" class="button simple  close-modal add-collection"> Add </a></div>';
		},
		col1: function(){
			var col = "<ol><img style='padding: 70px;' src='"+ptCollections.image+"'>";
			$.get("/api/collections/get?pid="+currentProject.id,function(res){
				
				if(res.error){
				
				}else{
					var pt_col1 = $(".pt-col1 ol");
					pt_col1.html('');
					$.each(res.data,function(x,y){
						
						pt_col1.append(ptCollections.html.li(y.name,y.id));
					});
					
				}

				
				
			}).error(function(data, status, headers, config) {
				if(status=="error") showStickyErrorToast(data.responseText);
				console.log(data);
				console.log(status);
				
				
			});
			
			

			
			col+= "</ol>";
			return col;
		},
		tabbar:function(){
			var tb = '<div class="pt-collection-bar">';
			tb+='<ul>';
				tb+='<li>';
					tb+='<a href="#" name="attributes">Attributes</a>';
				tb+='</li>';
				tb+='<li>';
					tb+='<a href="#" name="value">Values</a>';
				tb+='</li>';
			tb+='</ul>';
			tb+='</div>';
			return tb;
		},
		pool:function(){
			var imageLoading = '<div style="padding: 60px 10px 10px 200px; display:none" class="imageLoadingAttributes"><img src="'+ptCollections.image+'"></div>';
			var html = imageLoading+"<div class='poolAttributes' style='display:none;'>";
			html+='<div class="formaddcol_attr" style="padding: 10px;">';
			html+='<input type="text" name="name" style="line-height: 10px;"> <a href="javascript:void(0)"  class="button simple  close-modal add-collection-attr"> Save </a>';
			html+='</div>';
			html+= "<div class='poll' style='height:300px;overflow-y: scroll;'>";
			html+= '<table  id="pool_collection" class="table table-bordered table-striped pt-layouts">';
			html+= '<tbody>';
			html+= ptCollections.html.poolData([]);
			
			html+= '</tbody>';
			html+= '</table>';
			
			html+= "</div>";
			html+= "</div>";
			
			
			/** Values **/
			html+="<div class='poolValues'  style='display:none;'>";
			html+='<div class="formaddcol_value" style="padding: 10px;">';
			html+=ptCollectionsValues.rawForm([]);
			html+='</div>';
			html+= "<div class='poll' style='height:300px;overflow-y: scroll;'>";
			html+= '<table  id="pool_collection_value" class="table table-bordered table-striped pt-layouts">';
			html+= '<tbody>';
			
			
			html+= '</tbody>';
			html+= '</table>';
			
			html+= "</div>";
			html+= "</div>";
			
			return html;
		},
		poolData:function(d){
			
			if(d.length == 0){
				return "";
			}else{
				var html = "";
				$.each(d,function(x,y){
					
					html+=ptCollections.html.rowAttr(y);
				
				});
				return html;
					
				
			}
		},
		rowAttr:function(r){
			return ptCollectionsAttr.rows(r);
			
		}
	},
	actions:{
		paintTable:function(){
			
			var html = '';
			html+= '<div class="pt-col-board">';
				html+= '<div class="pt-col1">';
					html+= ptCollections.html.formAddCollection();
					html+= ptCollections.html.col1();
				html+= '</div>';
				html+= '<div class="pt-col2">';
					html+= ptCollections.html.tabbar();
					html+= ptCollections.html.pool();
				html+= '</div>';
			html+= '</div>';
			return html;
		},
		main:function(callback){
			if(typeof callback == "function"){
				callback();
			}
		},
		save:function(v){
			$.post("/api/collections/save?pid="+currentProject.id,{'name':v},function(res){
				if(res.error){
					showStickyErrorToast(res.message);
				}else{
					// showStickySuccessToast(res.message);
					var pt_col1 = $(".pt-col1 ol");
					pt_col1.prepend(ptCollections.html.li(v,res.data.id));
				}
			}).error(function(data, status, headers, config) {
				if(status=="error") showStickyErrorToast(data.responseText);
				console.log(data);
				console.log(status);
			});
		},
		saveAttr:function(v){
			$.post("/api/collections/saveAttr?pid="+ptCollections.id,{'name':v},function(res){
				ptCollectionsAttr.change = true;
				if(res.error){
					showStickyErrorToast(res.message);
				}else{
					var pt_col1 = $("#pool_collection tbody");
					pt_col1.append( ptCollections.html.rowAttr(res.data) );
				}
			}).error(function(data, status, headers, config) {
				if(status=="error") showStickyErrorToast(data.responseText);
				console.log(data);
				console.log(status);
			});
		},
		getAttrCollection:function(){
			$(".imageLoadingAttributes").show();
			$(".poolAttributes").hide();
			$(".poolValues").hide();
			$.get("/api/collections/getAttr?pid="+ptCollections.id,function(res){
				
				if(res.error){
				
				}else{
					/** Attributes **/
					$("#pool_collection tbody").html( ptCollections.html.poolData(res.data) );
					$(".poolAttributes").show();
					/** values **/
					
					$(".formaddcol_value").html( ptCollectionsValues.rawForm(res.data) );
					
					$(".pt-collection-bar ul li:first").addClass('active');
					$(".pt-collection-bar ul li:last").removeClass('active');
					
				}
				
				$(".imageLoadingAttributes").hide();
				
				
			}).error(function(data, status, headers, config) {
				if(status=="error") showStickyErrorToast(data.responseText);
				console.log(data);
				console.log(status);
				$(".imageLoadingAttributes").hide();
				
			});
		}
	}
};
$(".pt-col1 ol li a").live('click',function(e){
	var tid = $(this).attr('uuid');
	
	ptCollections.id = tid;
	
	$.each($(".pt-col1 ol li"),function(x,y){
		
		$(y).removeClass('active');
	});
	$(this).parent().addClass('active');
	
	ptCollections.actions.getAttrCollection();
});
$(".add-collection").live('click',function(e){
	var ipcollection = $(this).parent().find('input');
	if(ipcollection.val() == ""){
		ipcollection.focus().select();
		return false;
	}else{
		ptCollections.actions.save(ipcollection.val());
	}
});

$(".pt-collection-bar a").live('click',function(e){
	if(ptCollections.id == 0) {
		showStickyErrorToast('You need choice a collection.');
		return false;
	}
	$.each($(".pt-collection-bar li"),function(x,y){
		
		$(y).removeClass('active');
	});
	$(this).parent().addClass('active');
	
	
	var name = $(this).attr('name');
	
	switch(name){
		case "value":
			ptCollectionsValues.getValuesCollection();
			ptCollectionsValues.get();
			$(".poolValues").show();
			$(".poolAttributes").hide();
		break;
		default:
			$(".poolValues").hide();
			$(".poolAttributes").show();
		break;
	}
})
